<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" session="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
    <title>楼栋管理</title>

    <link href="${pageContext.request.contextPath}/static/css/common.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/css/bootstrap.min.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/font-awesome-4.3.0/css/font-awesome.min.css"
          rel="stylesheet">
    <!-- jqgrid-->
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/custom.ui.jqgrid.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/layer/skin/layer.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/style.css" rel="stylesheet">

    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery.form.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/common.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/i18n/grid.locale-cn.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/minified/jquery.jqGrid.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/layer/layer.js" type="text/javascript"></script>
    <script src="${pageContext.request.contextPath}/static/js/global.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/js/select2.min.js"></script>
	<script type="text/javascript"
            src="${pageContext.request.contextPath}/static/plugins/My97DatePicker/WdatePicker.js"></script>

    <style type="text/css">
        .main h5 a {
            position: absolute;
            right: 20px;
            font-size: 14px;
            bottom: 0px;
            display: inline-block;
            padding: 5px;
        }

        .table_list tbody tr {
            height: 35px;
            text-align: center;
        }
    </style>
</head>
<body view="menu-item-4142">
<jsp:include page="../../top.jsp"></jsp:include>
<div class="right">
    <div class="wrapper">
	    <div class="jqGrid_wrapper">
	        <div id="jqGrid_operation" class="jg-grid">
	            
	            <div class="jqGrid_operation" id="action"></div>
	        </div>
	        <table id="jqGrid"></table>
	        <div id="jqGrid_paper"></div>
	    </div>
	</div>
</div>
<script>
    $(document).ready(
            function () {
            	$.ajax({
                    type: "POST",
                    async: false,
                    url: '/admin/auth_menusAll.action',
                    data: "url=" + window.location.pathname,
                    success: function (data) {
                        data = eval("(" + data + ")");
                        for (var i in data) {
                            var action = data[i].mName;
                            switch (action) {
                                case "添加楼栋":
                                    $("#jqGrid_operation").append("<a style=\"border-bottom:1px solid #337ab7;margin-left: 14px;font-size: 16px;\" href=\"javascript:;\" onclick=\"javascript:post(\'${pageContext.request.contextPath}/admin/cbuilding_addView.action\',{communityId:\'${communityId}\'})\">批量添加楼栋</a>");
                                    break;
                                case "房间管理":
                                    $("#jqGrid_operation").append("<a style=\"border-bottom:1px solid #337ab7;margin-left: 14px;font-size: 16px;\" href=\"javascript:;\" onclick=\"javascript:post(\'${pageContext.request.contextPath}/admin/croom_listView.action\',{communityId:\'${communityId}\'})\">房间管理</a>");
                                    break;
                            }
                        }
                    }
                });
            	 $.ajax({
                     type: "POST",
                     async: false,
                     url: '/admin/auth_buttons.action',
                     data: "url=" + window.location.pathname,
                     success: function (data) {
                         data = eval("(" + data + ")");
                         for (var i in data) {
                             var action = data[i].mName;
                             switch (action) {
                                 case "单元新增":
                                     $("#action").append("<a id=\"add\"><i class=\"glyphicon glyphicon-plus\"></i>添加单元</a>");
                                     $("#add").click(function () {
                                         layer.open({
                                             type: 2,
                                             title: ['单元新增', 'font-weight:bold'],
                                             shadeClose: true, //点击遮罩关闭层
                                             area: ['800px', '430px'],
                                             scrollbar: false,
                                             content: '${pageContext.request.contextPath}/admin/cinstallunit_addView.action?communityId=${communityId}',
                                             btn: ['提交', '取消'],
                                             yes: function (index, layero) {
                                                 var options = {
                                                     success: function (data) {
                                                         data = JSON.parse(data);
                                                         if (data.code == 1) {
                                                             layer.alert(data.msg, function () {
                                                                 layer.closeAll();
                                                                 refreshGrid();
                                                             });
                                                         } else {
                                                             layer.alert(data.msg);
                                                         }
                                                     }
                                                 };
                                                 var cbName = $("#layui-layer-iframe" + index).contents().find("#cbName").val();
                                                	if(!cbName){
                                                		alert("请输入单元名称！");
                                                	}else{
                                                	 	$("#layui-layer-iframe" + index).contents().find("#form").ajaxForm(options);
                                               			$("#layui-layer-iframe" + index).contents().find("#form").submit();  	
                                                	}
                                             },
                                             cancel: function (index, layero) {
                                                 layer.close(index);
                                             }
                                         });
                                     });
                                     break;
                                 case "单元删除":
                                     $("#action").append("<a id=\"del\"><i class=\"glyphicon glyphicon-trash\"></i>删除</a>");
                                     $("#del").click(function () {
                                         var sels = jQuery("#jqGrid").jqGrid('getGridParam', 'selarrrow');
                                         if (sels == "") {
                                             layer.alert("请选择要删除的记录", {
                                                 icon: 6,
                                                 closeBtn: false
                                             });
                                         } else {
                                             layer.confirm('确认删除所选择的记录', function (index) {
                                                 $.post("${pageContext.request.contextPath}/admin/cinstallunit_delete.action", {
                                                     "ids": sels.toString()
                                                 }, function (result) {
                                                     result = eval("(" + result + ")");
                                                     layer.alert(result.msg);
                                                     refreshGrid();
                                                 });
                                                 layer.close(index);
                                             });
                                         }
                                     });
                                     break;
                             }
                         }
                     }
                 });
                 
                $.jgrid.defaults.styleUI = "Bootstrap";
                $("#jqGrid").jqGrid(
                        {
                            url: "${pageContext.request.contextPath}/admin/cinstallunit_list.action?communityId=${communityId}",
                            mtype: 'POST',
                            datatype: "json",
                            height: 400,
                            autowidth: true,
                            shrinkToFit: true,
                            rowNum: 10,
                            rowList: [10, 20, 30],
                            colNames: ['序号', '楼栋', '单元', '操作'],
                            colModel: [{
                                name: "SID",
                                index: "SID",
                                key: true,
                                hidden: true
                            }, {
                                name: 'COMM',
                                index: 'COMM',
                            }, {
                                name: 'CBNAME',
                                index: 'CBNAME',
                            },{
                                name: 'SID',
                                index: 'SID',
                                formatter: function (cellvalue, options, rawObject) {
                                	if(cellvalue == '-1'){
                                     	return '';
                                    }else{
                                         return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="修改" onclick="edit('
                                                 + rawObject.SID + ')">';
                                     }
                                }
                            }],
                            sortname: 'sid',
                            sortorder:'desc',
                            rownumbers: true,
                            jsonReader: {
                                root: 'rows',
                                page: "page",
                                total: 'total',
                                records: "records",
                                repeatitems: false
                            },
                            pager: "#jqGrid_paper",
                            viewrecords: true,
                            multiselect: true,
                            hidegrid: false,
                            loadComplete: function(){
                        		var re_records = Number($("#jqGrid").getGridParam('records'));
                        		var page = Number($("#jqGrid").getGridParam('page'));
                        		var rows=Number($("#jqGrid").getGridParam('rowNum'));
                        		var num = re_records-(page-1)*rows;
                        		if(num < rows)
                        		{
                        			for(var i=num;i<rows;i++){
                        				$("#jqGrid").jqGrid("addRowData",(i+1),
                                				{ "SID":"", "CBUILDINGID":"", "CBNAME":"", "SID":"-1"}
                        				,"");
                        			}
                        			$(".ui-paging-info").html(re_records-num+1 +" - "+re_records+"　共 "+re_records+" 条");
                        		}
                        	}
                        });
                
                <!--查询-->
                jQuery("#resetForm").click(resetForm);
            });

    <!--刷新jqGrid表格数据-->
    function refreshGrid() {
        $("#jqGrid").trigger("reloadGrid");
    }
    function resetForm() {
        location.reload();
        return false;
    }
    function edit(id){
    	layer.open({
            type: 2,
            title: ['修改楼栋', 'font-weight:bold'],
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '430px'],
            scrollbar: false,
            content: '${pageContext.request.contextPath}/admin/cinstallunit_editView.action?sid='+id,
            btn: ['提交', '取消'],
            yes: function (index, layero) {
                var options = {
                    success: function (data) {
                        data = JSON.parse(data);
                        if (data.code == 1) {
                            layer.alert(data.msg, function () {
                                layer.closeAll();
                                refreshGrid();
                            });
                        } else {
                            layer.alert(data.msg);
                        }
                    }
                };
                $("#layui-layer-iframe" + index).contents().find("#form").ajaxForm(options);
                $("#layui-layer-iframe" + index).contents().find("#form").submit();
            },
            cancel: function (index, layero) {
                layer.close(index);
            }
        });
    }
</script>
</body>
</html>
